<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\SystemStore;
use App\Traits\Controllers\AdminTraits;
use App\Traits\Controllers\UpdateStatusTraits;
use App\Transformers\SystemNotice\SystemStoreTransformer;
use Illuminate\Http\Request;
use PhpParser\Node\Expr\Cast\Object_;

class SystemStoreController extends Controller
{
    use AdminTraits;
    use UpdateStatusTraits;

    public $model = SystemStore::class;

    public $transformer = SystemStoreTransformer::class;

    /**
     * 提货点列表
     */
    public function index(Request $request)
    {
        $show = $request->input('status');
        $deleted = $request->input('deleted');
        $list = $this->getQuery()
            ->pimp()
            ->when($show, function ($query) use ($show) {
                $query->where('status', $show);
            })
            ->when($deleted, function ($query) {
                $query->onlyTrashed();
            })
            ->paginate($this->getPageLimit());
        return $this->response->paginator($list, new SystemStoreTransformer());
    }

    /**
     * 获取头部信息
     */
    public function get_header()
    {

        $count = $this->getQuery()
            ->selectRaw("COUNT(DISTINCT IF(status=1,id,NULL))as display,
            COUNT(DISTINCT IF(status=0,id,NULL))as hide")
            ->get();
        $deleted = $this->getQuery()
            ->where('deleted_at', 1)
            ->count();
        $data = [
            ['status' => 1, 'name' => '显示中的商品', 'count' => $count[0]->display],
            ['status' => 0, 'name' => '隐藏中的商品', 'count' => $count[0]->hide],
            ['deleted_at' => 1, 'name' => '回收站中的商品', 'count' => $deleted],
        ];
       return $this->response->array($data);

    }

    //修改
    public function setUpdate(SystemStore $systemStore, Request $request)
    {
        $systemStore->update($request->all());
        $systemStore->save();
        return $this->response->noContent();
    }

    /**
     * 参数验证规则
     *
     * @param Request $request
     * @return array
     */
    public function rules(Request $request): array
    {
        switch ($request->method()) {
            case 'GET':
                return [];
                break;
            case 'POST':
            case 'PUT':
                return [
                    'name' => 'required',
                    'image' => 'required',
                    'day_time' => 'required',
                    'phone' => 'required|regex:/^1[3-9]\d{9}$/',
                    'status' => 'required',
                    'address' => 'required',
                    'latitude' => 'required',
                    'longitude' => 'required',
                    'introduction' => 'required|max:255',
                    'detailed_address' => 'required|max:255',
                    'oblong_image' => 'required',
                    'valid_time' => 'required'

                ];
                break;
            case 'PATCH':
                return [
                    'status' => 'required'
                ];
                break;

        }
        return [];
    }

    /**
     * 验证信息
     *
     * @return array
     */
    public function ruleMessages(): array
    {
        return [
            'name.required' => '名称不能为空',
            'image.required' => '推荐图不能为空',
            'day_time.required' => '营业时间不能为空',
            'phone.required' => '手机号不能为空',
            'phone.regex' => '手机号码格式不正确',
            'status.required' => '状态不能为空',
            'address.required' => '地址不能为空',
            'address.exists' => '地址不存在',
            'latitude.required' => '纬度不能为空',
            'longitude.required' => '经度不能为空',
            'introduction.required' => '简介不能为空',
            'introduction.max' => '简介最大不能超过255字符',
            'detailed_address.required' => '详细地址不能为空',
            'detailed_address.max' => '详细地址最大不能超过255字符',
            'oblong_image.required' => '门店推荐图不能为空',
            'valid_time.required' => '有效时间不能为空'
        ];
    }


}
